package routes

import (
	"os"
	"strings"

	"github.com/gofiber/fiber/v2"
	"gorm.io/gorm"
	"main/src/controllers"
	"main/src/middlewares"
)

func User(app *fiber.App, db *gorm.DB) fiber.Router {
	users := app.Group("/user",
		middlewares.AddGlobalVarToRequestsMiddleware(db, []byte(os.Getenv("JWT_SECRET"))),
		middlewares.AllRequests)
	users.Post("/add",
		middlewares.ValidateAdminToken,
		middlewares.CheckRegRole(strings.Split(os.Getenv("USER_ROLES"), ",")),
		controllers.AddUserHandler)
	users.Post("/access", middlewares.ValidateToken, controllers.AccessHandler)
	users.Post("/login", controllers.LoginHandler)
	users.Post("/del/:username",
		middlewares.ValidateAdminToken,
		controllers.DelUserHandler)

	return users
}
